package com.yuanqi.repository;

import com.yuanqi.domain.CollectionRecord;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface CollectionRecordRepository extends JpaRepository<CollectionRecord, Integer> {
    
    Optional<CollectionRecord> findByCollectionCode(String collectionCode);
    
    List<CollectionRecord> findByCustomerNameContaining(String customerName);
    
    List<CollectionRecord> findByOrderNumber(String orderNumber);
    
    List<CollectionRecord> findByStatus(Integer status);
    
    @Query("SELECT c FROM CollectionRecord c WHERE " +
           "(:collectionCode IS NULL OR c.collectionCode LIKE %:collectionCode%) AND " +
           "(:customerName IS NULL OR c.customerName LIKE %:customerName%) AND " +
           "(:collectionDate IS NULL OR c.collectionDate = :collectionDate)")
    List<CollectionRecord> findByConditions(@Param("collectionCode") String collectionCode,
                                           @Param("customerName") String customerName,
                                           @Param("collectionDate") String collectionDate);
    
    long countByStatus(Integer status);
}
